#include "tomatoclock.h"
#include "ui_taskpage.h"
#include "taskpage.h"
#include "achieve.h"
#include "statistics.h"
#include "databasemanager.h"
#include "taskform.h"
#include "wode.h"
#include "createtask.h"
#include <QSqlQuery>

TaskPage *task;

TaskPage::TaskPage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::TaskPage)
{
    ui->setupUi(this);
    task=this;
    Achieve g_achieve;
    g_achieve.displayfruit(ui->label_3);

    //四个菜单按钮
    ui->start->setStyleSheet(
        "QPushButton {"
        "background-color: rgba(255, 255, 255, 128);"
        "color: #91ada2;"
        "}"
        );
    ui->wode->setStyleSheet(
        "QPushButton {"
        "background-color: rgba(255, 255, 255, 128);"
        "color: #91ada2;"
        "}"
        );
    ui->task->setStyleSheet(
        "QPushButton {"
        "background-color: rgba(255, 255, 255, 128);"
        "color: #91ada2;"
        "}"
        );
    ui->pushButton_4->setStyleSheet(
        "QPushButton {"
        "background-color: rgba(255, 255, 255, 128);"
        "color: #91ada2;"
        "}"
        );

    //＋号
    ui->pushButton_5->setStyleSheet(
        "QPushButton {"
        " background-color: transparent;"
        "color: white;"
        "}"
        );

    //@用户名
    ui->label_3->setStyleSheet(
        "QLabel {"
        " background-color: transparent;"
        "color: darkblue;"
        "}"
        );
    display();
}

TaskPage::~TaskPage()
{
    delete ui;
}

void TaskPage::on_start_clicked()
{
    tomatoclock *w=new tomatoclock;
    this->close();
    w->show();
}

void TaskPage::on_pushButton_4_clicked()
{
    Statistics *s=new Statistics;
    this->close();
    s->show();
}//切换到统计


void TaskPage::on_wode_clicked()
{
    Wode *w=new Wode;
    this->close();
    w->show();
}//切换到设置


void TaskPage::on_pushButton_5_clicked()
{
    CreateTask *c=new CreateTask;
    c->show();
}//添加

void TaskPage::display()
{
    qDebug()<<"task display begin";
    QSqlQuery query(DatabaseManager::getDatabase());
    QString sqlselect = "SELECT * FROM task WHERE user = ? ORDER BY ID DESC;";
    query.prepare(sqlselect);
    query.addBindValue(DatabaseManager::getUID());
    //遍历数据库记录
    if(!query.exec()){
        qDebug()<<"failed to query in task ";
    }
    ui->listWidget->clear();
    while(query.next()){
        QString task_name=query.value("task_name").toString();
        QString date=query.value("end_date").toString();
        qDebug()<<"display task ："<<date;
        //每遍历一条更新ui组件setItemInfo
        taskform *historyitem=new taskform;
        historyitem->settaskinfo(task_name,date);
        QListWidgetItem*item=new QListWidgetItem;
        item->setSizeHint(QSize(550,120));
        //将每行与数据表连接
        ui->listWidget->addItem(item);
        ui->listWidget->setItemWidget(item,historyitem);
    }

}


